import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo_数组求和3 {
    static int[] arr = new int[10000000];
    static int num1 = 0;
    static int num2 = 0;

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100) + 1;
        }
        FutureTask<Integer> futureTask1 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                for (int i = 0; i < arr.length; i+=2) {
                    num1 = num1 + arr[i];
                }
                return num1;
            }
        });
        Thread t1 = new Thread(futureTask1);
        t1.start();

        FutureTask<Integer> futureTask2 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                for (int i = 1; i < arr.length; i+=2) {
                    num2 = num2 + arr[i];
                }
                return num2;
            }
        });
        Thread t2 = new Thread(futureTask2);
        t2.start();

        t1.join();
        t2.join();
        int resault = futureTask1.get() + futureTask2.get();
        System.out.println(resault);
    }
}
